capture log close
*the working directory is set at 
cd "C:\Users\jmryan\Dropbox\Palin Effect\work\data"


log using "05 palineffectwknuckeydata.log",replace text

//  program:	Palin Effect regression models
//  task:       This takes the data sent by Knuckey to us to duplicate his results
//				for the R&R
//  project:    Palin Effect
//  author:     Josh Ryan \ May-June 2013

// #Start
// program setup

version 11.1
clear all 
set linesize 80
macro drop _all
set seed 123456
set scheme s2mono

use "knuckey-palindata.dta", clear

*our original model used in first table
*logit votedv palintherm repid demid natlecon moderate liberal conservative oimage1 mcimage1 [pweight=presampw]
* see palineffectdatasetup.do for how we generated our marginal effects in the submission, just substituting our code
*and using knuckey's data
*using knuckey's data, same variables, just different names
logit vote palin rep dem natecon mod lib con obaimg macimg [pweight=Weight_variable]

gen indid=0
replace indid=1 if dem==0 & rep==0
gen indpalin=indid*palin

*dem is taken out, replaced with indid because it must be included as constituent term
logit vote palin rep indid indpalin natecon mod lib con obaimg macimg [pweight=Weight_variable]
* marginal effects for party ID
* where indid is x, indpalin is xz, and the * is multiplied by the observed value of z
* each of the values is copied into excel file located at: C:\Users\jmryan\Dropbox\Palin Effect\work\data\indrepmargins
*tabs are changing party id, changing ideology, under knuckey replication columns
* saved as substantive effects

lincom (indid*1 + indpalin*0)
lincom (indid*1 + indpalin*.10)
lincom (indid + indpalin*.20)
lincom (indid + indpalin*.30)
lincom (indid + indpalin*.40)
lincom (indid + indpalin*.50)
lincom (indid + indpalin*.60)
lincom (indid + indpalin*.70)
lincom (indid + indpalin*.80)
lincom (indid + indpalin*.90)
lincom (indid *1+ indpalin*1)

gen reppalin=rep*palin
logit vote palin rep indid reppalin natecon mod lib con obaimg macimg [pweight=Weight_variable]

lincom (rep + reppalin*0)
lincom (rep + reppalin*.10)
lincom (rep + reppalin*.20)
lincom (rep + reppalin*.30)
lincom (rep + reppalin*.40)
lincom (rep + reppalin*.50)
lincom (rep + reppalin*.60)
lincom (rep + reppalin*.70)
lincom (rep + reppalin*.80)
lincom (rep + reppalin*.90)
lincom (rep + reppalin*1.00)

*dropped into dataset below as re-*
save "krep-partyandideomargins.dta", replace



*********************************************************************************************
************************let's do the same for ideology
use "knuckey-palindata.dta", clear

*original model
logit vote palin rep dem natecon mod lib con obaimg macimg [pweight=Weight_variable]

gen ideopalin=mod*palin
*model with interaction term generated above
logit vote palin rep dem ideopalin natecon mod lib con obaimg macimg [pweight=Weight_variable]

lincom (mod + ideopalin*0)
lincom (mod + ideopalin*.10)
lincom (mod + ideopalin*.20)
lincom (mod + ideopalin*.30)
lincom (mod + ideopalin*.40)
lincom (mod + ideopalin*.50)
lincom (mod + ideopalin*.60)
lincom (mod + ideopalin*.70)
lincom (mod + ideopalin*.80)
lincom (mod+ ideopalin*.90)
lincom (mod + ideopalin*1.00)

*model with conservative ideology generated
gen conspalin=con*palin
logit vote palin rep dem conspalin natecon mod lib con obaimg macimg [pweight=Weight_variable]

lincom (con + conspalin*0)
lincom (con + conspalin*.10)
lincom (con + conspalin*.20)
lincom (con + conspalin*.30)
lincom (con + conspalin*.40)
lincom (con + conspalin*.50)
lincom (con + conspalin*.60)
lincom (con + conspalin*.70)
lincom (con + conspalin*.80)
lincom (con+ conspalin*.90)
lincom (con + conspalin*1.00)

use "krep-partyandideomargins.dta", clear

*put marginal effects in 
*C:\Users\jmryan\Dropbox\Palin Effect\work\data\partyandideomargins.xls
save "krep-partyandideomargins.dta", replace


use "krep-partyandideomargins.dta", clear

twoway (connected ideocoef obsval if ideo==1, sort lcolor(black) lpattern(dash) lwidth(thin) msymbol(none)) ///
(connected ideolowerci obsval if ideo==1, sort lcolor(black) lpattern(dash) lwidth(thin) msymbol(none)) ///
(connected ideoupperci obsval if ideo==1, sort lcolor(black) lpattern(dash) lwidth(thin) msymbol(none)) ///
(connected ideocoef obsval if ideo==0, sort lcolor(black) lpattern(dot) lwidth(thick) msymbol(none)) ///
(connected ideolowerci obsval if ideo==0, sort lcolor(black) lpattern(dot) lwidth(thick) msymbol(none)) ///
(connected ideoupperci obsval if ideo==0, sort lcolor(black) lpattern(dot) lwidth(thick) msymbol(none)), ///
ytitle("Marginal Effect") xtitle(" " "Palin Feeling Thermometer") ///
 ylabel(-2(1)6, valuelabel) ///
xlabel(0(10)100, valuelabel) legend(rows(1) order(1 "Moderate w/ 95% CI" 4 "Conservative w/ 95% CI" ) ///
position(3:30) ring(0) size(small)) scheme(s1mono) graphregion(fcolor(white) ifcolor(white)) title("") 
graph save krep-ideopalin.gph, replace

twoway (connected partycoef obsval if partyid==1, sort lcolor(black) lpattern(dash) lwidth(thin) msymbol(none)) ///
(connected partylowerci obsval if partyid==1, sort lcolor(black) lpattern(dash) lwidth(thin) msymbol(none)) ///
(connected partyupperci obsval if partyid==1, sort lcolor(black) lpattern(dash) lwidth(thin) msymbol(none)) ///
(connected partycoef obsval if partyid==0, sort lcolor(black) lpattern(dot) lwidth(thick) msymbol(none)) ///
(connected partylowerci obsval if partyid==0, sort lcolor(black) lpattern(dot) lwidth(thick) msymbol(none)) ///
(connected partyupperci obsval if partyid==0, sort lcolor(black) lpattern(dot) lwidth(thick) msymbol(none)), ///
ytitle("Marginal Effect") xtitle(" " "Palin Feeling Thermometer") ///
 ylabel(-2(1)6, valuelabel) ///
xlabel(0(10)100, valuelabel) legend(rows(1) order(1 "Independent w/ 95% CI" 4 "Republican w/ 95% CI" ) ///
position(3:30) ring(0) size(small)) scheme(s1mono) graphregion(fcolor(white) ifcolor(white))  title("") 
graph save krep-partyidpalin.gph, replace

graph combine krep-ideopalin.gph krep-partyidpalin.gph , col(1) iscale(.75) graphregion(fcolor(white)) scheme(s2mono)
graph export "C:\Users\jmryan\Dropbox\Palin Effect\work\writing\k-repideoandpartypalininteract.eps", replace
*****************************************************************************8
use "knuckey-palindata.dta", clear

*Two versions of the question, first set is for version j, second set is for version k
*factor honest intel know care leader moral optimist
rename V083099a dmoral1
rename V083099b dstrong1
rename V083099c dcare1
rename V083099d dknow1
rename V083099e dintel1
rename V083099g doptimist1 
rename V083099f  dhonest1

*get rid of missing. -1 equals other version so that's left in
recode dmoral1 -9=. -8=. 
recode dstrong1 -9=. -8=. 
recode dcare1 -9=. -8=. 
recode dknow1 -9=. -8=. 
recode dintel1 -9=. -8=. 
recode doptimist1 -9=. -8=. 
recode dhonest1 -9=. -8=. 

*code on a zero to one scale
replace dmoral1=(dmoral1-1)/3
tab dmoral1
replace dstrong1=(dstrong1-1)/3
tab dstrong1
replace dcare1=(dcare1-1)/3
tab dcare1
replace dknow1=(dknow1-1)/3
tab dknow1
replace dintel1=(dintel1-1)/3
tab dintel1
replace doptimist1=(doptimist1-1)/3
tab doptimist1
replace dhonest1=(dhonest1-1)/3
tab dhonest1

*now do the same for version 2
rename V083100a dmoral2
rename V083100b dstrong2
rename V083100c dcare2
rename V083100d dknow2
rename V083100e dintel2
rename V083100g doptimist2 
rename V083100f  dhonest2

recode dmoral2 -9=. -8=. 
recode dstrong2 -9=. -8=. 
recode dcare2 -9=. -8=. 
recode dknow2 -9=. -8=. 
recode dintel2 -9=. -8=. 
recode doptimist2 -9=. -8=. 
recode dhonest2 -9=. -8=. 

replace dmoral2=(dmoral2-1)/4
tab dmoral2
replace dstrong2=(dstrong2-1)/4
tab dstrong2
replace dcare2=(dcare2-1)/4
tab dcare2
replace dknow2=(dknow2-1)/4
tab dknow2
replace dintel2=(dintel2-1)/4
tab dintel2
replace doptimist2=(doptimist2-1)/4
tab doptimist2
replace dhonest2=(dhonest2-1)/4
tab dhonest2

*now combine two versions into one measure, where -1 in version one is replaced with version 2
replace dmoral1=dmoral2 if dmoral1<0
tab dmoral1
replace dstrong1=dstrong2 if dstrong1<0
tab dstrong1
replace dcare1=dcare2 if dcare1<0
tab dcare1
replace dknow1=dknow2 if dknow1<0
tab dknow1
replace dintel1=dintel2 if dintel1<0
tab dintel1
replace doptimist1=doptimist2 if doptimist1<0
tab doptimist1
replace dhonest1=dhonest2 if dhonest1<0
tab dhonest1

*predict the factor
factor dhonest1 dintel1 dknow1 dcare1 dstrong1 dmoral1 doptimist1
predict factor1

corr factor1 obaimg

*put on a 0 to 1 scale
replace factor1=(factor1+2.476398)/3.750496
sum factor1

*I don't know, trying to make the obs line up, which they do after this but it doesn't matter anyway
*because with a weight of zero they aren't even included
replace factor1=. if Weight_variable==0

*very very close
logit vote palin rep dem natecon mod lib con factor1 macimg [pweight=Weight_variable]
logit vote palin rep dem natecon mod lib con obaimg macimg [pweight=Weight_variable]



************************************effect sizes graphs

use "04 vpcoefsize.dta", clear

		
*note var7 is used as y  because i had to mess with bar size to fit in lieberman							  
#delimit ;
twoway (scatter coeff var7, lcolor(none) fcolor(gs14)) (rcap upperci lowerci var7, lcolor(black) lwidth(vthin) fcolor(none)), 
yline(3.34418, lpattern(vshortdash)) yline(5.71275, lpattern(vshortdash))
 ytitle("Effect Size", size()) xtitle("") ylabel(0(1)7, valuelabel labsize())
 xlabel(1 `""Mondale" "1980""' 2.2 `""Bush" "1980"' 3.4 `""Ferraro" "1984""' 4.6 `""Bush" "1984""' 5.8 `""Bentsen" "1988""' 7.1 `""Quayle" "1988""' ///
 8.2 `""Gore" "1992""' 9.4 `""Quayle" "1992""' 10.6 `""Gore" "1996""' 11.8 `""Kemp" "1996""' 13.2 `""Lieberman" "2000""' 14.6 `""Cheney" "2000""' ///
 16 `""Edwards" "2004""' 17.4 `""Cheney" "2004""' 18.6 `""Biden" "2008""' 19.8 `""Palin" "2008""',valuelabel labsize(medium))
 legend(col(1) order(1 "Effect Size" 2 "95% Confidence Interval")
 position(6) size() title("Revised Effect of Vice Presidential Evaluations on Vote Choice, 1980-2000" "With Confidence Intervals",size())) scheme(s1mono)
 graphregion(fcolor(white) ifcolor(white)) title("VP Effect Sizes Including Confidence Intervals")
;
#delimit cr
*labeled vpeffectswse2 to indicate it's from the revision, post R&R		  
graph save vpeffectswse2.gph, replace

					  

graph combine vpeffectswse2.gph , col(1) scale(.5) graphregion(fcolor(white)) scheme(s2mono)
graph export "C:\Users\jmryan\Dropbox\Palin Effect\work\writing\coefsizeswse2.eps", replace










